﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for CompanyInfoHandler
    /// </summary>
    public class CompanyInfoHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetCompanyInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetCompanyInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "AddCompanyInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseAddCompanyInfo(context,Params);
                            }
                        }
                        break;
                    case "DelCompanyInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelCompanyInfo(context, Params);
                            }
                        }
                        break;
                    case "SaveCompanyInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveCompanyInfo(context, Params);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetCompanyInfo(HttpContext context, string Params)
        {
            try
            {
                IList<CompanyInfo> list = ProxyCompanyInfo.QueryCompanyInfo(WebHelper.GetCurrentUserID(), 0);
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseAddCompanyInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                CompanyInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r=JsonHelper.JsonDeserialize<CompanyInfo>(Params);
                    if (r != null)
                    {
                        ProxyCompanyInfo.InsertCompanyInfo(WebHelper.GetCurrentUserID(), 1, 0,r.CompanyName, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelCompanyInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                CompanyInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<CompanyInfo>(Params);
                    if (r != null)
                    {
                        ProxyCompanyInfo.InsertCompanyInfo(WebHelper.GetCurrentUserID(), 3, r.AutoID, r.CompanyName, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveCompanyInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                CompanyInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<CompanyInfo>(Params);
                    if (r != null)
                    {
                        ProxyCompanyInfo.InsertCompanyInfo(WebHelper.GetCurrentUserID(), 2, r.AutoID, r.CompanyName, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}